//
//  AddPayViewController.m
//  quickBatchManager
//
//  Created by jack on 15/10/15.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "AddPayViewController.h"
#import "PMCalendar.h"
#import "PayModel.h"
@interface AddPayViewController ()<PMCalendarControllerDelegate>
{
    NSMutableArray *array;
    NSInteger payTypeId;
}
@property (nonatomic, strong) PMCalendarController *pmCC;
@end

@implementation AddPayViewController
@synthesize pmCC;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)initUI
{
    
    _remarkField.layer.borderWidth = 1;
    _remarkField.layer.borderColor = [FONT_COLOR_GRAY CGColor];
    _remarkField.layer.cornerRadius = 5.0f;
    _remarkField.layer.masksToBounds = YES;
    _closeBtn.userInteractionEnabled = YES;
    [_closeBtn bk_whenTapped:^{
        [self dismissViewControllerAnimated:YES completion:nil];
        [_delegate AddPayRefresh:self];
    }];
    
    [_exitBtn bk_whenTapped:^{
        [self dismissViewControllerAnimated:YES completion:nil];
         [_delegate AddPayRefresh:self];
    }];
    
    self.view.backgroundColor = [UIColor clearColor];
    
    [_addPayTypeBtn bk_whenTapped:^{
        NSLog(@"第一.....");
        if([_addPayTypeField.text isValid])
        {
              NSLog(@"第er.....");
            LKDBHelper *helper = [LKDBHelper getUsingLKDBHelper];
            PayTypeModel *typeModel = [[PayTypeModel alloc] init];
            typeModel.payTypeName = _addPayTypeField.text;
            [helper insertToDB:typeModel];
            [_payTypeTableView reloadData];
            
        }
    }];
    
    [_comfirmBtn bk_whenTapped:^{
        if(![_payTypeField.text isValid])
        {
            [self showAlert:@"请选择支出用途"];
            return;
        }
        
        if(![_moneyField.text isValid])
        {
            [self showAlert:@"请输入支出金额"];
            return;
        }
        
        LKDBHelper *helper = [LKDBHelper getUsingLKDBHelper];
        PayModel *model = [[PayModel alloc] init];
        model.date  = _dateLabel.text;
        model.payTypeId = payTypeId;
        model.money = [_moneyField.text doubleValue];
        model.remark = _remarkField.text;
        model.date = [DateUtils dateToStringByFormat:[NSDate date] format:YYYYMMDDHHMMSS];
        [helper insertToDB:model];
        [self dismissViewControllerAnimated:YES completion:nil];
         [_delegate AddPayRefresh:self];
    }];
    
    _dateLabel.userInteractionEnabled = YES;
    [_dateLabel bk_whenTapped:^{
        self.pmCC = [[PMCalendarController alloc] initWithThemeName:@"default"];
        self.pmCC.delegate = self;
        self.pmCC.mondayFirstDayOfWeek = NO;
        if ([self.pmCC isCalendarVisible])
        {
            [self.pmCC dismissCalendarAnimated:NO];
        }
        [self.pmCC presentCalendarFromView:_dateLabel
                  permittedArrowDirections:PMCalendarArrowDirectionAny
                                 isPopover:YES
                                  animated:YES];
        self.pmCC.period = [PMPeriod oneDayPeriodWithDate:[NSDate date]];
        [self calendarController:pmCC didChangePeriod:pmCC.period];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    LKDBHelper *helper = [LKDBHelper getUsingLKDBHelper];
    array = [helper search:[PayTypeModel class] where:nil orderBy:nil offset:0 count:0];
    if(array!=nil)
        return [array count];
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
   NSString *reuseIdetify = @"PayTypeTableViewCell";
    PayTypeTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];
    if(!cell){
        
        
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"PayTypeTableViewCell" owner:self options:nil];
        for(id oneObject in nib){
            if([oneObject isKindOfClass:[PayTypeTableViewCell class]]){
                cell = (PayTypeTableViewCell *) oneObject;
            }
        }
        
    }

    PayTypeModel *model = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = model.payTypeName;
    
    cell.tag = indexPath.row;
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] title:@"删除"];
    [cell setRightUtilityButtons:rightUtilityButtons];
    cell.delegate = self;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     PayTypeModel *model = [array objectAtIndex:indexPath.row];
    payTypeId = model.t_id;
    _payTypeField.text = model.payTypeName;
    
}

- (void)calendarController:(PMCalendarController *)calendarController didChangePeriod:(PMPeriod *)newPeriod
{
    _dateLabel.text = [NSString stringWithFormat:@"%@"
                      , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM-dd"]];
}


- (void)calendarControllerDidDismissCalendar:(PMCalendarController *)calendarController
{
//    NSLog(@"日历关闭");
//    LKDBHelper *helpser = [LKDBHelper getUsingLKDBHelper];
//    SaleOrderModel *model = [helpser searchSingle:[SaleOrderModel class] where:[NSString stringWithFormat:@"saleOrderNo='%@'", mSaleOrderNo] orderBy:nil];
//    model.date = dateLabel.text;
//    [helpser updateToDB:model where:[NSString stringWithFormat:@"saleOrderNo='%@'", mSaleOrderNo]];
//    [clientTableView reloadData];
}


- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
    PayTypeModel *model = [array objectAtIndex:cell.tag];
    [model deleteToDB];
    [self.payTypeTableView reloadData];
}

@end
